package com.sleep.psy.external_server.business.main.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sleep.psy.external_server.business.main.entity.MainScheme;
import com.sleep.psy.external_server.business.main.entity.dto.SchemeInfo;
import com.sleep.psy.external_server.business.main.mapper.MainSchemeMapper;
import com.sleep.psy.external_server.business.main.service.MainSchemeService;
import com.sleep.psy.external_server.business.psy.entity.PsyBaseVideo;
import com.sleep.psy.external_server.business.psy.service.PsyBaseVideoService;
import com.sleep.psy.external_server.business.resourse.entity.ResourseScale;
import com.sleep.psy.external_server.business.resourse.mapper.ResourseScaleMapper;
import com.sleep.psy.external_server.constants.MySqlConstant;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sleep.psy.external_server.result.Result;
import com.sleep.psy.external_server.result.ResultEnum;
import com.sleep.psy.external_server.result.ResultUtil;
import com.sleep.psy.external_server.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 方案表 服务实现类
 * </p>
 *
 * @author wenliguo
 * @since 2025-08-13
 */
@Service
public class MainSchemeServiceImpl extends ServiceImpl<MainSchemeMapper, MainScheme> implements MainSchemeService {

    protected Logger logger = LoggerFactory.getLogger(MainSchemeService.class);

    @Autowired
    private MainSchemeMapper mainSchemeMapper;

    @Autowired
    private PsyBaseVideoService baseVideoService;

    @Autowired
    private ResourseScaleMapper resourseScaleMapper;

    /**
     * 通过方案id查询信息
     * @author wenliguo
     * @date 2025/8/13 10:11
     * @param id 方案id
     * @return com.sleep.psy.external_server.main.entity.MainScheme
     */
    @Override
    public MainScheme selectById(Integer id) {
        return mainSchemeMapper.selectById(id);
    }

    /**
     * 通过方案获取里面具体的量表、音频、视频
     * @author wenliguo
     * @date 2025/8/13 11:04
     * @param id 方案id
     * @return java.util.HashMap<java.lang.String,java.lang.Object>
     */
    @Override
    public HashMap<String, Object> selectInfoById(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("scaleList", new ArrayList<>());
        map.put("audioList", new ArrayList<>());
        map.put("videoList", new ArrayList<>());
        MainScheme scheme = mainSchemeMapper.selectById(id);
        if (null == scheme) {
            return null;
        }

        if (MySqlConstant.STATUS_0.equals(scheme.getStatus())) {
            logger.info("方案状态不可用,id={}", id);
            return null;
        }

        String startDate = scheme.getStartDate();
        String endDate = scheme.getEndDate();
        Date date = new Date();
        if (null != startDate) {
            Date beginDate = DateUtils.stringToDate(startDate, DateUtils.DATE_FORMAT);
            if (beginDate.before(date)) {
                logger.info("时间还未开始");
                return null;
            }
        }

        if (null != endDate) {
            Date stopDate = DateUtils.stringToDate(endDate, DateUtils.DATE_FORMAT);
            if (stopDate.after(date)) {
                logger.info("时间已过期");
                return null;
            }
        }

        //量表
        ArrayList<ResourseScale> scales = new ArrayList<>();
        String scaleIds = scheme.getScaleIds();
        if (null != scaleIds) {
            if (scaleIds.contains(",")) {

                String[] split = scaleIds.split(",");
                for (String s : split) {
                    ResourseScale scale = resourseScaleMapper.selectById(Integer.parseInt(s));
                    if (null != scale) {
                        scales.add(scale);
                    }
                }

            } else {
                ResourseScale scale = resourseScaleMapper.selectById(Integer.parseInt(scaleIds));
                if (null != scale) {
                    scales.add(scale);
                }
            }
            map.put("scaleList", scales);
        }

        //音频
        String audioIds = scheme.getAudioIds();
        ArrayList<PsyBaseVideo> videos = new ArrayList<>();
        if (null != audioIds) {
            if (audioIds.contains(",")) {
                String[] split = audioIds.split(",");
                for (String s : split) {
                    PsyBaseVideo video = baseVideoService.selectById(Integer.parseInt(s));
                    if (null != video) {
                        videos.add(video);
                    }
                }
            } else {
                PsyBaseVideo video = baseVideoService.selectById(Integer.parseInt(audioIds));
                if (null != video) {
                    videos.add(video);
                }
            }

            map.put("audioList", videos);
        }

        //视频
        String videoIds = scheme.getVideoIds();
        ArrayList<PsyBaseVideo> baseVideos = new ArrayList<>();
        if (null != videoIds) {
            if (videoIds.contains(",")) {
                String[] split = videoIds.split(",");
                for (String s : split) {
                    PsyBaseVideo video = baseVideoService.selectById(Integer.parseInt(s));
                    if (null != video) {
                        baseVideos.add(video);
                    }
                }
            } else {
                PsyBaseVideo video = baseVideoService.selectById(Integer.parseInt(videoIds));
                if (null != video) {
                    baseVideos.add(video);
                }
            }
            map.put("videoList", baseVideos);
        }

        return map;
    }

    /**
     * 通过id获取方案详情
     * @author wenliguo
     * @date 2025/8/17 10:23
     * @param id id
     * @return com.sleep.psy.external_server.result.Result
     */
    @Override
    public Result selectSchemeById(Integer id) {
        MainScheme scheme = selectById(id);
        if (null == scheme) {
            return ResultUtil.error(ResultEnum.NOT_SCHEME);
        }

        String schemeInfo = scheme.getSchemeInfo();
        SchemeInfo info = JSONObject.parseObject(schemeInfo, SchemeInfo.class);

        Integer status = info.getStatus();
        if (MySqlConstant.STATUS_0.equals(status)) {
            return ResultUtil.error(ResultEnum.SCHEME_STATUS_0);
        }

        Date date = new Date();

        String startDate = info.getStartDate();
        if (null != startDate) {
            Date sDate = DateUtils.stringToDate(startDate, DateUtils.DATE_FORMAT);
            if (date.before(sDate)) {
                return ResultUtil.error(ResultEnum.NO_START_DATE);
            }
        }

        String endDate = info.getEndDate();
        if (null != endDate) {
            Date eDate = DateUtils.stringToDate(endDate, DateUtils.DATE_FORMAT);
            if (eDate.after(date)) {
                return ResultUtil.error(ResultEnum.NO_END_DATE);
            }
        }

        return ResultUtil.success(info);
    }
}
